
package xj.graph2d.app.actions;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;

import xj.graph2d.app.DrawApp;
import xj.graph2d.app.DrawAction;

import static xj.app.BaseApplicationFrame.drawFrameIcon;
import static xj.app.BaseApplicationFrame.getResourceString;
import static xj.app.BaseApplicationFrame.getSystemProperty;

public class AboutListener extends DrawAction implements ActionListener {

  public void actionPerformed(ActionEvent e) {
    if (tabbedPane == null) {
      makeTabbedPane();
    }
    String title = getResourceString("AboutDialog.title") + " " + main.getAppName();
    JOptionPane.showMessageDialog(main.getJFrame(), 
				  tabbedPane, 
				  title, JOptionPane.INFORMATION_MESSAGE);
  }

  private JComponent makeAboutTab() { 
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    //panel.setBorder(new CompoundBorder(new EmptyBorder(5,5,5,5), new EtchedBorder()));
    //panel.setBorder(new EmptyBorder(5,5,5,5));
    panel.setBorder(new CompoundBorder(new CompoundBorder(new EmptyBorder(5,5,5,5), new EtchedBorder()),
				       new EmptyBorder(5,5,5,5)));

    String author = getSystemProperty("AUTHOR");
    String copyright = getSystemProperty("COPYRIGHT");
    String copyyear = getSystemProperty("COPYYEAR");

    String bdate =  getSystemProperty("DATE");
    String buser =  getSystemProperty("USER");
    String bosname = getSystemProperty("OSNAME");
    String bosarch = getSystemProperty("OSARCH");
    String bosversion = getSystemProperty("OSVERSION");
    String bjdk = getSystemProperty("JDKVERSION");

    JLabel[] message = new JLabel[6];
    message[0] = new JLabel(main.getAppName() + ", version " + main.getVersion() + " (" + 
			    main.getReleaseDate() + ")");
    message[1] = new JLabel(author);
    message[2] = new JLabel(getResourceString("Copyright.message") + " " + copyright + " " + copyyear); 
    message[3] = null;
    message[4] = new JLabel("Build " + main.getBuild() + " [" + bdate + "]");
    message[5] = new JLabel("Built on " + bosname + "-" + bosarch + " " + bosversion + " JDK " + bjdk);
    for (int i = 0; i < 6; i++) { 
      if (message[i] != null) { 
	panel.add(message[i]);
      }
    }
    return panel; 
  }

  private JComponent makeLicenseTab() {
    JTextArea textArea = new JTextArea();
    JScrollPane js = new JScrollPane(textArea); 
    try {
      InputStream in = 
	AboutListener.class.getResourceAsStream("/resources/graph2d/app/LICENSE.txt");
      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
      String line;
      StringBuilder sb = new StringBuilder();  
      while ((line = reader.readLine()) != null) {
	sb.append(line).append("\n"); 
      }
      textArea.setText(sb.toString()); 
      in.close();
    } catch (IOException e) {
    }

    textArea.setEditable(false);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setCaretPosition(0);
    //textArea.setFont(new Font("Sans Serif", Font.PLAIN, 10)); 

    js.setBorder(new EmptyBorder(5,5,5,5));
    return js;
  }

  private JComponent makeSysPropTab() {
    Properties props = System.getProperties();
    int size = props.size();
    String[][] rowData = new String[size][2];
    Iterator entries = props.entrySet().iterator();
    int rowNum = 0;
    while (entries.hasNext()) {
      Map.Entry entry = (Map.Entry) entries.next();
      rowData[rowNum][0] = (String) entry.getKey();
      rowData[rowNum][1] = (String) entry.getValue();
      rowNum++;
    }
    Arrays.sort(rowData,new java.util.Comparator<String[]>() {
      public int compare(String[] o1, String[] o2) {
        return o1[0].compareTo(o2[0]);
      }
    });
    String[] nvStrings = new String[]{"Name","Value"};
    UneditableTableModel model = new UneditableTableModel(rowData, nvStrings);
    JTable propertiesTable = new JTable(model);
    JScrollPane scroller = new JScrollPane(propertiesTable);
    scroller.setBorder(new EmptyBorder(5,0,0,0));
    JPanel propTab = new JPanel(new BorderLayout());
    propTab.add(new JLabel("Current system properties:"),BorderLayout.NORTH);
    propTab.add(scroller,BorderLayout.CENTER);
    propTab.setBorder(new CompoundBorder(new EmptyBorder(5,5,5,5), new EtchedBorder()));
    return propTab;
  }

  private void makeTabbedPane() { 
    tabbedPane = new JTabbedPane();
    tabbedPane.add("About", makeAboutTab());
    tabbedPane.add("License", makeLicenseTab());
    tabbedPane.add("System Properties", makeSysPropTab());
    //tabbedPane.add("License", new JButton("License"));
    tabbedPane.setPreferredSize(new Dimension(540, 300));
  }

  JTabbedPane tabbedPane; 

}

class UneditableTableModel extends DefaultTableModel {
  public UneditableTableModel() { super(); }
  public UneditableTableModel(int rowCount, int columnCount) { super(rowCount,columnCount); }
  public UneditableTableModel(Object[][] data, Object[] columnNames) { super(data, columnNames); }
  public UneditableTableModel(Vector columnNames, int rowCount) { super(columnNames, rowCount); }
  public UneditableTableModel(Vector data, Vector columnNames) { super(data,columnNames); }
  public boolean isCellEditable(int row, int col) { return false; }
}

